package com.ddpt.platform.common.export;

import com.ddpt.platform.common.base.BaseEntity;
import com.ddpt.platform.common.util.AssertUtil;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@Getter
@Setter
public class AsyncExportBean extends BaseEntity {

    //异步的服务类型
    private Set<ExportAppEnum> exportAppEnums = new HashSet<>();

    //传递的值
    private String val;

    //任务投递时间
    private Long timestamp;

    //流水号
    private String serialNumber;

    //是否强一致性，要求必须送达
    private boolean consistency;

    public AsyncExportBean build(ExportAppEnum app, String t){
        return build(app, t,null);
    }

    public AsyncExportBean build(ExportAppEnum app, String t, String serialNumber){
        return build(app, t,serialNumber,false);
    }

    public AsyncExportBean build(ExportAppEnum app, String t, String serialNumber, boolean consistency){
        AssertUtil.isNull(app,"导出应用类型不能为空");
        AssertUtil.isNull(t,"参数不能为空");
        if(StringUtils.isBlank(serialNumber)){
            serialNumber = UUID.randomUUID().toString().replace("-","");
        }
        AsyncExportBean bean = new AsyncExportBean();
        bean.setSerialNumber(serialNumber);
        bean.setConsistency(consistency);
        bean.getExportAppEnums().add(app);
        bean.setVal(t);
        bean.setTimestamp(System.currentTimeMillis());
        return bean;
    }

    public AsyncExportBean addApps(ExportAppEnum...apps){
        if(apps.length == 0){
            return this;
        }
        this.getExportAppEnums().addAll(Arrays.asList(apps));
        return this;
    }
}
